/** navbar.js By lishida
 **/
layui.define(['element', 'common'], function(exports) {
	"use strict";
    var $ = layui.jquery,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        element = layui.element(),
        common = layui.common,
        cacheName = 'tb_navbar';

    var Navbar = function() {
        /**
         *  默认配置
         */
        this.config = {
            elem: undefined, //容器
            data: undefined, //数据源
            url: undefined, //数据源地址
            type: 'GET', //读取方式
            cached: false //是否使用缓存
        };
        this.v = '0.0.1';
    };
    Navbar.prototype.render = function(nav_index,result) {
        var _that = this;
        var _config = _that.config;
        if(typeof(_config.elem) !== 'string' && typeof(_config.elem) !== 'object') {
            common.throwError('Navbar error: elem参数未定义或设置出错，具体设置格式请参考文档API.');
        }
        var $container;
        if(typeof(_config.elem) === 'string') {
            $container = $('' + _config.elem + '');
        }
        if(typeof(_config.elem) === 'object') {
            $container = _config.elem;
        }
        if($container.length === 0) {
            common.throwError('Navbar error:找不到elem参数配置的容器，请检查.');
        }
        if(_config.data === undefined && _config.url === undefined) {
            common.throwError('Navbar error:请为Navbar配置数据源.')
        }
        if(_config.data !== undefined && typeof(_config.data) === 'object') {
            var html = getHtml(_config.data);
            $container.html(html[nav_index-1]);
            element.init();
            _that.config.elem = $container;
        } else {
            if(_config.cached) {
                var cacheNavbar = layui.data(cacheName);
                if(cacheNavbar.navbar === undefined) {
                    layui.data(cacheName, {
                        key: 'navbar',
                        value: result
                    });
                    var html = getHtml(result);
                    $container.html(html[nav_index]-1);
                    element.init();
                } else {
                    var html = getHtml(cacheNavbar.navbar);
                    $container.html(html);
                    element.init();
                    _that.config.elem = $container;
                }
            } else {
                //清空缓存
                layui.data(cacheName, null);
                var html = getHtml(result);
                $container.html(html[nav_index]-1);
                element.init();
            }
        }
        return _that;
    };
    /**
     * 配置Navbar
     * @param {Object} options
     */
    Navbar.prototype.set = function(options) {
        var that = this;
        that.config.data = undefined;
        $.extend(true, that.config, options);
        return that;
    };
    /**
     * 绑定事件
     * @param {String} events
     * @param {Function} callback
     */
    Navbar.prototype.on = function(events, callback) {
        var that = this;
        var _con = $("#side");
        if(typeof(events) !== 'string') {
            common.throwError('Navbar error:事件名配置出错，请参考API文档.');
        }
        var lIndex = events.indexOf('(');
        var eventName = events.substr(0, lIndex);
        var filter = events.substring(lIndex + 1, events.indexOf(')'));
        if(eventName === 'click') {
            if(_con.attr('lay-filter') !== undefined) {
                _con.children('ul').find('li').each(function() {
                    var $this = $(this);
                    if($this.find('dl').length > 0) {
                        var $dd = $this.find('dd').each(function() {
                            $(this).on('click', function() {
                                var $a = $(this).children('a');
                                var href = $a.data('url');
                                var icon = $a.children('i:first').data('icon');
                                var title = $a.children('cite').text();
                                var data = {
                                    elem: $a,
                                    field: {
                                        href: href,
                                        /*icon: icon,*/
                                        title: title
                                    }
                                }
                                callback(data);
                            });
                        });
                    } else {
                        $this.on('click', function() {
                            var $a = $this.children('a');
                            var href = $a.data('url');
                            var icon = $a.children('i:first').data('icon');
                            var title = $a.children('cite').text();
                            var data = {
                                elem: $a,
                                field: {
                                    href: href,
                                    /*icon: icon,*/
                                    title: title
                                }
                            }
                            callback(data);
                        });
                    }
                });
            }
        }
    };
    /**
     * 清除缓存
     */
    Navbar.prototype.cleanCached = function(){
        layui.data(cacheName,null);
    };
    /**
     * 获取html字符串
     * @param {Object} data
     */
    function getHtml(result) {
        var a =[];
        for(var i = 0; i < result.length-2; i++) {
            var ulHtml = '<ul class="layui-nav layui-nav-tree navbar_background">';
            if(result[i].isParent == true){
            	for (var j=0;j<result[i].children.length;j++){
                	if(j==0){
                		 ulHtml += ' <li class="layui-nav-item layui-nav-itemed">';
                	}else{
                		ulHtml += ' <li class="layui-nav-item">';
                	}
                    ulHtml += ' <a href="javascript:;">'+result[i].children[j].name+'</a>';
                    if(result[i].children[j] !== undefined && result[i].children[j].children.length > 0){
                        ulHtml += '<dl class="layui-nav-child">'
                        for (var k = 0;k<result[i].children[j].children.length;k++){
                            ulHtml += '<dd>';
                            ulHtml += '<a href="javascript:;" data-url="../adminthemes/version3/new_table.html">';//设置对应选项卡的url (暂无)
                            ulHtml += '<cite>' + result[i].children[j].children[k].name + '</cite>';
                            ulHtml += '</a>';
                            ulHtml += '</dd>';
                        }
                        ulHtml += '</dl>';
                    }
                    ulHtml += '</li>';
                }	
            }else{
            	 ulHtml += ' <li class="layui-nav-item layui-nav-itemed">';
            	 ulHtml += ' <a href="javascript:; data-url="../core/admin/floor/list.do"">'+result[i].name+'</a>';
            	 ulHtml += '</li>';
            }
            
            ulHtml += '</ul>';

            a[i]=ulHtml;
        }
        return a;
    }

    var navbar = new Navbar();

    exports('navbar', function(options) {
        return navbar.set(options);
    });
});
